package com.example.list;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class AddViewActivity2 extends AppCompatActivity {

    private LinearLayout ll_purple;
    private TextView tvAddview2;
    private TextView tvAddview22;
    private Button btnAddview2;
    private Button btnAddview3;

    private TextView tvAddviewRed;
    private TextView tvAddview33;
    private LinearLayout llAddview3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_view2);



        ll_purple = (LinearLayout) findViewById(R.id.ll_purple);
        tvAddview2 = (TextView) findViewById(R.id.tv_addview2);
        tvAddview22 = (TextView) findViewById(R.id.tv_addview22);
        btnAddview2 = (Button) findViewById(R.id.btn_addview2);
        btnAddview3 = (Button) findViewById(R.id.btn_addview3);

        btnAddview2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addView();
            }
        });



        llAddview3 = (LinearLayout) findViewById(R.id.ll_addview3);
        tvAddviewRed = (TextView) findViewById(R.id.tv_addviewRed);
        tvAddview33= (TextView) findViewById(R.id.tv_addView33);

        btnAddview3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addView3();
            }
        });

    }

    private void addView() {

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 3.0f);
        lp.gravity=Gravity.CENTER; //设置动态居中显示
        tvAddview2.setLayoutParams(lp);
        // get TextView02设置权重是2
        lp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 2.0f);
        lp.gravity=Gravity.CENTER;
        ll_purple.setLayoutParams(lp);

    }
    private void addView3() {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
        lp.gravity=Gravity.END;
        tvAddviewRed.setLayoutParams(lp);

        lp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
        lp.gravity=Gravity.LEFT;
        lp.leftMargin=50;
        tvAddview33.setLayoutParams(lp);
//
//        llAddview3.addView(tvAddviewRed);
//        llAddview3.addView(tvAddview33);


    }
}